package thread;

import java.util.concurrent.*;

public class TestThread05 implements Callable<Boolean> {

    private int ticket = 10;

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestThread05 ticket = new TestThread05();

        ExecutorService ser = Executors.newFixedThreadPool(3);
        Future<Boolean> r1 = ser.submit(ticket);
        Future<Boolean> r2 = ser.submit(ticket);
        Future<Boolean> r3 = ser.submit(ticket);
        boolean r01 = r1.get();
        boolean r02 = r2.get();
        boolean r03 = r3.get();

        ser.shutdownNow();
    }

    @Override
    public Boolean call(){
        while(true){
            if(this.ticket <= 0){
                break;
            }

            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName() + "抢到了第"
                    + ticket-- + "张票");
        }
        return true;
    }
}
